// pages/money/money.js
//获取应用实例
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,
    data:'',
    coupon_info:"",
    family_ecah_number:"",
    //活动规则内容
    info: "",
    recovery_order: "",
    num: '',
    animpurse: {},
    rules: true,
    model: false,//模态框
    active_rule: false,//活动规则
    person: false,//个人立减金
    close: false,//亲友立减金
    num: 10,
    images:{},
  },
  //模态框点击事件
  bation: function () {
    var animation = wx.createAnimation({duration: 0,delay: 0})
    this.animation = animation;
    var aaa = -100 +'%';
    animation.top(aaa).rotateX(10).step();
    this.setData({
      raise_lower: animation.export(),
      model: false,//模态框
      active_rule: false,//活动规则
      person: false,//个人立减金
      close: false,//个人立减金
    })
  },

  imageLoad: function (e) {
    var $width = e.detail.width,    //获取图片真实宽度
      $height = e.detail.height,
      ratio = $width / $height;    //图片的真实宽高比例
    var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
      viewHeight = 718 / ratio;    //计算的高度值
    var image = this.data.images;
    //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
    image[0] = {
      width: viewWidth,
      height: viewHeight
    }
    console.log(image);
    this.setData({
      images: image
    })
  },
  // 活动规则
  rule_click: function () {
    this.setData({
      active_rule: true,
      model: true
    })
  },

  //领取个人立减金
  people: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var recovery_order = that.data.recovery_order;
    if (recovery_order[id].click > 0) {
      recovery_order[id].click--;
      wx.request({
        url: app.url + 'api_reduce_money/add_reduce_money',
        data: {
          id: that.data.id,
          type:1, // 个人立减金
          // sign:sign,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          console.log(res);
          that.setData({
            coupon_info: res.data.result,
          })
        }
      })
      that.lower_animation();
      setTimeout(function () {
      that.setData({
        recovery_order: recovery_order,
        model: true,//模态框
        person: true,//个人立减金
      })
      },1000)
    } else {
      wx.showModal({
        title: '提示',
        content: '立减金领到啦，快去用吧~',
      })
    }
  },
  //去使用立减金
  go_home: function () {
    console.log('进入首页');
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  // 商品推荐列表
  click_goods: function (e) {
    wx.navigateTo({
      url: '/pages/index/detail/index?id=' + e.currentTarget.id,
    })
  },

  //领取亲友立减金
  canles: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var recovery_order = that.data.recovery_order;
    if (recovery_order[id].click > 0) {
      recovery_order[id].click--;
      wx.request({
        url: app.url + 'api_reduce_money/add_reduce_money',
        data: {
          id: that.data.id,
          type: 2, // 亲友立减金
          // sign:sign,
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        }, success: function (res) {
          console.log(res);
          if (!res.data.done){
            if (res.data.message == 10) {
              recovery_order[id].click++;
              that.setData({
                recovery_order: recovery_order,
              })
              res.data.message = '好友还没领立减金呢，快去提醒他';
              app.apperror(res);
            } else {
              res.data.message = '领取失败';
              app.apperror(res);
            }
          } else {
            that.setData({
              family_ecah_number: res.data.result.family_ecah_number,
              sign: res.data.result.sign,
            })

            that.lower_animation();
            setTimeout(function () {
              that.setData({
                recovery_order: recovery_order,
                model: true,//模态框
                close: true,//亲友立减金
              })
            }, 1000)
          }
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '您已经领取完咯 ~'
      })
    }
  },

  // 立减金动画
  lower_animation: function () {
    var animation = wx.createAnimation({
      duration: 1000,
      delay: 0
    })
    this.animation = animation;
    animation.top(0).rotateX(10).step();
    this.setData({
      raise_lower: animation.export(),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var that = this;
    wx.request({
      url: app.url + 'api_reduce_money/receive_reduce_money',
      data: {
        id: options.id,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (res) {
        console.log(res.data);
        that.setData({
          id: options.id,
          data: res.data.result,
          recovery_order: [{click: res.data.result.use_individual_number }, {click:res.data.result.use_family_number}],
          goods_list: res.data.result.goods_list,
          goods_nav_img: res.data.result.goods_nav_img,
        })
        if (res.data.message==8){
          wx.showModal({
            title: '提示',
            content: '抱歉，该活动已结束',
          })
        }
      }
    })
  },
// 钱袋点击
money_click:function(){
  wx.navigateTo({
    url: '/pages/reducemoney/discount/index?id='+this.data.id,
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var i = 0;
    setInterval(function () {
      ++i;
      var animation = wx.createAnimation({
        duration: 200,
      })
      that.animation = animation
      if (i % 2 == 0) {
        animation.scale(1, 1).step()
      } else {
        animation.scale(1.05, 1.05).step()
      }
      that.setData({
        animpurse: animation.export(),
      })
    }, 350)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var that = this;
    e.target.dataset.sign = that.data.sign;
    console.log(e.target.dataset.sign)

    var share_img = that.data.data.share_img;
    var imageUrl = that.data.imgurl + share_img.substring(1);
    return {
      imageUrl: imageUrl,
      title: that.data.data.title, // 标题
      path: '/pages/reducemoney/detail/index?id=' + that.data.data.id + '&sign=' + e.target.dataset.sign, // 分享后点击页面链接
      success: function (res) {

      }
    }
  }
})